package edu.zju.gis.dbfg.server.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.TaskExecutor;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

@Configuration
@EnableAsync
public class ThreadPoolConfig {

    @Autowired
    private CommonSetting setting;

    @Bean("taskExecutor")
    public TaskExecutor taskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(setting.getInitTaskPoolSize());
        executor.setMaxPoolSize(setting.getMaxTaskPoolSize());
        executor.setQueueCapacity(setting.getQueueCapacity());
        executor.setKeepAliveSeconds(setting.getKeepAliveSeconds());
        executor.setThreadNamePrefix("dbfg-parallel-");
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        return executor;
    }
}
